'''
建立代理ip池 - 私密代理
'''
import requests
from fake_useragent import UserAgent

class ProxyPool:
    def __init__(self):
        self.api_url = 'http://dps.kdlapi.com/api/getdps/?orderid=962572654773341&num=20&pt=1&sep=1'
        self.test_url = 'http://httpbin.org/get'

    def get_html(self, url, proxies={}):
        """请求功能函数"""
        headers = {'User-Agent':UserAgent(path='fake_useragent.json').random}
        resp = requests.get(url=url, proxies=proxies, headers=headers, timeout=3)

        return resp

    def get_proxy(self):
        """获取代理ip,并依次测试"""
        html = self.get_html(self.api_url).text
        # proxy_list:['IP:PORT','IP:PORT',...]
        proxy_list = html.split('\r\n')
        for proxy in proxy_list:
            # 测试proxy代理IP是否可用
            self.check_proxy(proxy)

    def check_proxy(self, proxy):
        """测试1个代理IP是否可用函数"""
        proxies = {
            'http':'http://309435365:szayclhp@' + proxy,
            'https': 'https://309435365:szayclhp@' + proxy,
        }
        try:
            resp = self.get_html(self.test_url, proxies)
            print(proxy,'\033[31mis ok\033[0m')
        except Exception as e:
            print(proxy, 'is not ok')

    def crawl(self):
        self.get_proxy()

if __name__ == '__main__':
    spider = ProxyPool()
    spider.crawl()















